import { login, getInfo } from '@/api/login'
import {
  ZHENGLIUJI_ACCESS_TOKEN,
  ADMIN_PLATFORM_CODE,
  ADMIN_PLATFORM_NAME,
  ADMIN_PLATFORM_USERINFO
} from '@/utils/index'
import storage from 'store'
const STORAGE_ROUTES = 'hxh-storage-routes'

const user = {
  state: {
    token: '',
    name: '',
    welcome: '',
    avatar: '',
    roles: [],
    info: {}
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, { name, welcome }) => {
      state.name = name
      state.welcome = welcome
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_INFO: (state, info) => {
      state.info = info
    }
  },

  actions: {
    // 登录
    Login ({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        login(userInfo)
          .then((res) => {
            console.log(res,'login')
            const token = res.data.token
            storage.set('token', token)
            storage.set('user_id', res.data.id)
            storage.set('userInfo', {name: userInfo.name,...res.data})
            commit('SET_INFO', {name: userInfo.name,...res.data})
            commit('SET_NAME', { name: 'name' })
            commit('SET_TOKEN', token)
            resolve(res.data)
          })
          .catch((error) => {
            reject(error)
          })
      })
    },
    Login2 ({ commit }, userInfo) {
      console.log(userInfo)
      return new Promise((resolve, reject) => {
        const {
          token
        } = userInfo
        console.log(token)
        storage.set(ZHENGLIUJI_ACCESS_TOKEN, token)
        storage.set(ADMIN_PLATFORM_CODE, userInfo.platform_code)
        storage.set(ADMIN_PLATFORM_NAME, userInfo.platform_name)
        storage.set('use_level', userInfo.user_level)
        storage.set('permission_level', userInfo.user_level)
        storage.set('industry_code', '')

        storage.set(ADMIN_PLATFORM_USERINFO, userInfo)
        if (userInfo.user_level == 2) {
          storage.set('global_merchant_code', userInfo.merchant_code)
          storage.set('global_merchant_name', userInfo.merchant_name)
        } else if (userInfo.user_level == 3) {
          storage.set('global_store_code', userInfo.store_code)
          storage.set('global_store_name', userInfo.store_name)
          storage.set('global_merchant_code', userInfo.merchant_code)
          storage.set('global_merchant_name', userInfo.merchant_name)
          storage.set('industry_code', userInfo.industry_code.join(','))
        }
        commit('SET_TOKEN', token)
        resolve()
      })
    },
    Logout () {
      return new Promise((resolve) => {
        storage.remove('token')
        storage.remove('user_id')
        storage.remove(STORAGE_ROUTES)
        storage.remove(ADMIN_PLATFORM_CODE)
        storage.remove(ADMIN_PLATFORM_NAME)
        resolve()
      })
    },
    // 获取用户信息
    GetInfo ({ commit }) {
      return new Promise((resolve, reject) => {
        const res = {
          data: {
            role: 'admin'
          }
        }
        // storage.set('userInfo', res.data)
        commit('SET_INFO', storage.get('userInfo'))
        commit('SET_NAME', { name: res.data.name })
        resolve(res.data)
        commit('SET_ROLES', {})

      //   getInfo()
      //     .then((res) => {
      //       console.log(res)
      //       res.data.role = 'admin'
      //       storage.set('userInfo', res.data)
      //       commit('SET_INFO', res.data)
      //       commit('SET_NAME', { name: res.data.name })
      //       resolve(res.data)
      //       commit('SET_ROLES', {})
      //     })
      //     .catch((err) => {
      //       reject(err)
      //     })
      })
    }

  }
}

export default user
